//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    hiddens: true,
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    allshops:[
      {
       name:"清吧",
       image:"../../image/all0.png",
       id:"0"
      },
      {
        name: "慢摇吧",
        image: "../../image/all1.png",
        id: "1"
      },
      {
        name: "限量KTV",
        image: "../../image/all2.png",
        id: "2"
      },
      {
        name: "商务会所",
        image: "../../image/all3.png",
        id: "3"
      },
      {
        name: "其他",
        image: "../../image/all4.png",
        id: "4"
      },
      {
        name: "电视塔",
        image: "../../image/all5.png",
        id: "5"
      },
      {
        name: "九眼桥",
        image: "../../image/all6.png",
        id: "6"
      },
      {
        name: "兰桂坊",
        image: "../../image/all7.png",
        id: "7"
      },
    ]
  },

  onLoad: function (options) {
    var that = this;
    this.setData({
      imageUrl: app.globalData.imageUrl
    })
    app.showShareMenus();
    var scene = decodeURIComponent(options.scene);
    if (scene != 'undefined') {
      app.globalData.referrerId = scene;
      useUserInfo(this);
    }else{
      if (options.id) {
        app.globalData.referrerId = options.id;
        useUserInfo(this);
      } else {
        useUserInfo(this);
      }
    }
  },
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
 turnToSearch: function () {
    wx.navigateTo({
      url: '../../pages/searchShops/searchShops',
    })
  }
  
  , onShow: function () {
    getShops(this);
    host_goods(this);
    getProducts(this);
    getIndexBanner(this)
    goods_class(this,'慢摇吧')
    goods_class(this, '限量KTV')
    goods_class(this, '商务会所')
    goods_class(this, '其他')
    goods_class(this, '清吧')
  },
  onShareAppMessage: function (res) {
    return {
      title: '酒吧订位打折兰桂坊九眼桥少陵路',
      path: '/pages/homepage/homepage?id=' + wx.getStorageSync("userid"),
    }
  }
  , toShopDetail:function(e){
    console.log(e.target.id)
    wx.navigateTo({
      url: '../../pages/singleShopProduct/singleShopProduct?id='+e.target.id,
    })
  }
  , toallGoods: function (e) {
    wx.navigateTo({
      url: '../../pages/allGoods/allGoods?id=' + e.target.id,
    })
  }
  , toProductsDetail:function(e){
    wx.navigateTo({
      url: '../../pages/directDetail/directDetail?id=' + e.target.id,
    })
  }
  , toAllShops: function () {
    wx.switchTab({
      url: '../../pages/moreSeller/moreSeller',
    })
  }
  , toAllGoods:function(){
     wx.navigateTo({
       url: '../../pages/allGoods/allGoods',
     })
  }
  , toall4:function(e){
    console.log(e.target.id)
    if(e.target.id=="0"){
      this.toAllGoods();
    } else if (e.target.id == "1"){
      wx.switchTab({
        url: '../../pages/moreSeller/moreSeller',
      })
    } else if (e.target.id == "2") {
      wx.navigateTo({
        url: '../../pages/coupon/coupon',
      })
    } else if (e.target.id == "3") {
      wx.navigateTo({
        url: '../../pages/aboutCompany/aboutCompany',
      })
    }
  }
})


function getDirectProduct(that, id) {
  var _classifyBanner = [];
  wx.request({
    url: app.globalData.allUrl + "/directAction/getDirectProduct",
    data: {
    //  directId: 1,
      pageNum: 1,
      pageCount: 30,
      classify: id,
      putaway: 1
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
   //   console.log(res)
     if(id==1){
       that.setData({
         classifyProduct0: res.data
       })
     } else if (id == 2) {
       that.setData({
         classifyProduct1: res.data
       })
     } else if (id == 3) {
       that.setData({
         classifyProduct2: res.data
       })
     } else if (id == 4) {
       that.setData({
         classifyProduct3: res.data
       })
     } else if (id == 5) {
       that.setData({
         classifyProduct4: res.data
       })
     } else if (id == 6) {
       that.setData({
         classifyProduct5: res.data
       })
     } else if (id == 7) {
       that.setData({
         classifyProduct6: res.data
       })
     }
       
      setLists(that);
    }
  })
}

function getIndexBanner(that) {
  wx.request({
    url: app.globalData.allUrl + "/user/image",
    data: {
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
     // console.log(res.data)
      that.setData({
        imgUrls: res.data
      })
    }
  })
}

function getPersonLocation(thisid) {//
  var that = thisid;
  wx.getLocation({
    success: function (res) {
      wx.request({
        url: app.globalData.allUrl + "/utilAction/getmapinfo",
        data: {
          latitude: res.latitude,
          longitude: res.longitude
        },
        success: function (ops) {
          wx.setStorageSync('checekgetPersonLocation', 1)
          if (that.data.noRoot) {
            that.setData({
              hasRoot: false,
              noRoot: true
            })
          }
          //console.log(ops.data)
          var newJson = ops.data.indexOf("{")
          var nextJson = ops.data.lastIndexOf(")")
          var userJson = ops.data.substring(newJson, nextJson)
          var res = JSON.parse(userJson)
          var addressDetaile = res.result.addressComponent.province + res.result.addressComponent.city + res.result.addressComponent.district
          that.setData({
            district: res.result.addressComponent.district,
            adcode: res.result.addressComponent.adcode,
          })
          app.addressDetailes = addressDetaile;
          app.district = res.result.addressComponent.district
          app.adcode = res.result.addressComponent.adcode;
          wx.setStorageSync('adcode', res.result.addressComponent.adcode)
        }
      })
    }, fail: function () {
      wx.setStorageSync('checekgetPersonLocation', 0)
      that.setData({
        hasRoot: true,
        noRoot: false
      })
    }
  })

}

function getShops(that){
  wx.request({
    url: app.globalData.allUrl + "/shop/shop_stress",
    data: {
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
       console.log(res.data)
      that.setData({
        allstreet: res.data.data
      })
    }
  })
}

function host_goods(that) {
  wx.request({
    url: app.globalData.allUrl + "/goods/host_goods",
    data: {
      types:'推荐'
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      //console.log(res.data)
      that.setData({
        shop1: res.data
      })
    }
  })
}

function goods_class(that,_classify) {
  wx.request({
    url: app.globalData.allUrl + "/goods/goods_class",
    data: {
      classify: _classify,
      count:4
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (_classify=="慢摇吧"){
        that.setData({
          shop2: res.data
        })
      } else if (_classify =="限量KTV") {
        that.setData({
          shop3: res.data
        })
      } else if (_classify == "商务会所") {
        that.setData({
          shop4: res.data
        })
      } else if (_classify == "其他") {
        that.setData({
          shop5: res.data
        })
      } else if (_classify == "清吧") {
        that.setData({
          shop6: res.data
        })
      }   
    }
  })
}

function getProducts(that) {
  wx.request({
    url: app.globalData.allUrl + "/shop/goodslist",
    data: {
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
    //  console.log(res.data)
      that.setData({
        allProducts: res.data.list
      })
    }
  })
}

function useUserInfo(that) {
  if (wx.getStorageSync('userid')) {
    that.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
  } else {
    //调用登录接口
    wx.login({
      success: function (rese) {
        wx.getUserInfo({
          success: function (res) {
            app.globalData.hasRoot = false;
            app.globalData.noRoot = true;
            wx.setStorageSync('userInfo', res.userInfo)//将用户信息储存在本
            app.globalData.userInfo = res.userInfo;
            var reg = /[A-Za - z0 - 9_\-\u4e00 - \u9fa5]+/
            if (!reg.test(res.userInfo.nickName)) {
              res.userInfo.nickName == "默认用户名"
            }
            that.setData({
              userInfo: res.userInfo
            })
            wx.request({
              url: app.globalData.allUrl + "/login/login",
              data: {
                code: rese.code,
              },
              header: { 'content-type': "application/x-www-form-urlencoded" },
              method: 'POST',
              success: function (datas) {
                console.log(datas.data)
                wx.request({
                  url: app.globalData.allUrl + "/user/saveUserInfo",
                  data: {
                    user_id: datas.data.openid,
                    user_name: res.userInfo.nickName,
                    image_url: res.userInfo.avatarUrl,
                  //  pid: app.globalData.referrerId,
                  //  pid:"o6EMU0dmBI65mKguVWmW3eiR7AM1"
                  },
                  header: { 'content-type': "application/x-www-form-urlencoded" },
                  method: 'POST',
                  success: function (res) {
                    console.log(res)
                    wx.setStorageSync('userid', datas.data.openid)
                  }, fail: function () {
                  }
                })
              }, fail: function () {
              }
            })
          }, fail: function () {
            wx.showModal({
              title: '提示',
              content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
              success: function (res) {
                if (res.confirm) {
                  wx.openSetting({
                    success: (res) => {
                      if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
                        wx.getUserInfo({
                          success: function (res) {
                            app.globalData.hasRoot = false;
                            app.globalData.noRoot = true;
                            wx.setStorageSync('userInfo', res.userInfo)//将用户信息储存在本
                            app.globalData.userInfo = res.userInfo;
                            var reg = /[A-Za - z0 - 9_\-\u4e00 - \u9fa5]+/
                            if (!reg.test(res.userInfo.nickName)) {
                              res.userInfo.nickName == "默认用户名"
                            }
                            that.setData({
                              userInfo: res.userInfo
                            })
                            wx.request({
                              url: app.globalData.allUrl + "/login/login",
                              data: {
                                code: rese.code,
                              },
                              header: { 'content-type': "application/x-www-form-urlencoded" },
                              method: 'POST',
                              success: function (datas) {
                                wx.request({
                                  url: app.globalData.allUrl + "/login/saveUserInfo.php",
                                  data: {
                                    user_id: datas.data.openid,
                                    user_name: res.userInfo.nickName,
                                    image_url: res.userInfo.avatarUrl,
                                  //  pid: app.globalData.referrerId,
                                  },
                                  header: { 'content-type': "application/x-www-form-urlencoded" },
                                  method: 'POST',
                                  success: function (res) {
                                    wx.setStorageSync('userid', datas.data.openid)
                                  }, fail: function () {
                                  }
                                })
                              }, fail: function () {
                              }
                            })
                          }
                        })
                      }
                    }, fail: function (res) {
                    }
                  })
                }
              }
            })
          }
        })
      }
    })
  }

}